﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.PersonalInfo
{
    public partial class UpLoadPortrait : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            string msg = string.Empty;
            string type = Request.QueryString["Type"];
            if (type == "upload")
            {
                int albumID = 0;
                System.Web.HttpFileCollection file = System.Web.HttpContext.Current.Request.Files;
                Jy123.Common.Upload upload = new Jy123.Common.Upload();
                int isNullCount = 0;//空文件数量的计数器
                HttpPostedFile postFile = file[0];
                string photoPath = postFile.FileName;
                if (string.IsNullOrEmpty(photoPath)) { isNullCount += 1; }//如果出现文件为空，计数器+1
                if (isNullCount < file.Count)
                {
                    string upLoadedPath = UploadPath(userID, postFile,upload);//上传并返回上传后的路径
                    if (!string.IsNullOrEmpty(upLoadedPath))
                    {
                        Jy123.Model.User_Photo mPhoto = new Jy123.Model.User_Photo();
                        Jy123.BLL.User_Photo bllPhoto = new Jy123.BLL.User_Photo();
                        mPhoto.FAlbum = albumID;
                        mPhoto.FUser = userID;
                        mPhoto.Description = "无描述";
                        mPhoto.Views = 0;
                        mPhoto.FileSize = upload.FileLength;
                        mPhoto.State = 0;
                        mPhoto.IsCover = false;
                        mPhoto.Comments = 0;
                        mPhoto.PostTime = DateTime.Now;
                        mPhoto.PostIP = Common.Public.GetClientIP();
                        mPhoto.IsLock = false;
                        mPhoto.PhotoType = 1;//先上传到相册
                        mPhoto.FilePath = upLoadedPath;
                        int affectRows = bllPhoto.Add(mPhoto);
                        //加入动态
                        Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                        Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                        mUserForState = bllUserForState.GetModel(userID);

                        Jy123.Model.User_Photo mPhotoForState = new Jy123.Model.User_Photo();
                        Jy123.BLL.User_Photo bllPhotoForState = new Jy123.BLL.User_Photo();
                        mPhotoForState = bllPhotoForState.GetModel(affectRows);
                        new Jy123.BLL.Public.State().CreateModifyUserState(mUserForState, mPhotoForState, (int)Jy123.BLL.Public.EnumConfig.StateActionType.ModifyUserPhoto);
                        //加入动态
                        Response.Redirect("/PersonalInfo/CutPortrait.aspx?photoID="+affectRows);
                    }
                }
                else
                {
                    msg = "<script>$(function(){$.moonbox.warning('文件为空，不可上传!');})</script>";
                }
            }

            //页面初始化
            StringBuilder sbHtml = new StringBuilder(Cache["user_uploadportrait"].ToString());//最终写出页面的大sb
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            //int photoID = new Jy123.BLL.User.Album().GetPortraitID(userID);

            //sbHtml = sbHtml.Replace("$PhotoFilePath$", new Jy123.BLL.User_Main().GetModel(userID).Portrait);
            if (string.IsNullOrEmpty(new Jy123.BLL.User_Main().GetModel(userID).Portrait))
            {
                sbHtml = sbHtml.Replace("$PhotoFilePath$", new Jy123.BLL.User.SPace().DefaultPortrait(userID));
                sbHtml = sbHtml.Replace("请点这里裁剪当前头像", string.Empty);
                sbHtml = sbHtml.Replace("如果您不需要上传头像，只对现在的头像进行裁剪。",string.Empty);
            }
            else
            {
                sbHtml = sbHtml.Replace("$PhotoFilePath$", new Jy123.BLL.User_Main().GetModel(userID).Portrait);
            }
            
            Response.Write(sbHtml);
        }
        /// <summary>
        /// 上传单个照片
        /// </summary>
        private string UploadPath(int userID, HttpPostedFile postFile,Jy123.Common.Upload upload)
        {
             //= new Jy123.Common.Upload();
            string upLoadedPath = string.Empty;
            AppCode.CreateDir createDir = new Jy123.Web.AppCode.CreateDir();
            //System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            bool fileOK = false;
            //HttpPostedFile[] postFile = new HttpPostedFile[upfiles.Length];
            //HttpPostedFile postFile = files[0];// FileUpload1.PostedFile;
            string photoPath = postFile.FileName;
            string photoName = string.Empty;

            if (upload.IsPic(photoPath))//判断文件是否为图片类型
            {
                fileOK = true;
            }
            else
            {
                Response.Write("文件必须为.gif, .png, .bmp, .jpg等类型");
            }
            if (fileOK)
            {
                string photoType = photoPath.Substring(photoPath.LastIndexOf(".") + 1);
                photoName = createDir.CreatePhotoPath(Jy123.Web.AppCode.CreateDir.USER, userID, DateTime.Now, photoType);
                string mapPath = Server.MapPath("/");

                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = photoName;
                upload.Extension = photoType;
                upload.SavePath = mapPath;
                upload.UploadStart(photoName);
                upLoadedPath = photoName;
            }
            return upLoadedPath;
        }
    }
}
